{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 'else' for loop is confusing. Let's avoid it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loop 0\n",
      "Loop 1\n",
      "Loop 2\n",
      "Else block!\n"
     ]
    }
   ],
   "source": [
    "# Example 1\n",
    "for i in range(3):\n",
    "    print('Loop %d' % i)\n",
    "else:\n",
    "    print('Else block!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loop 0\n",
      "Loop 1\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "for i in range(3):\n",
    "    print('Loop %d' % i)\n",
    "    if i == 1:\n",
    "        break\n",
    "else:\n",
    "    print('Else block!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For Else block!\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "for x in []:\n",
    "    print('Never runs')\n",
    "else:\n",
    "    print('For Else block!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "While Else block!\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "while False:\n",
    "    print('Never runs')\n",
    "else:\n",
    "    print('While Else block!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Testing 2\n",
      "Testing 3\n",
      "Testing 4\n",
      "Coprime\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "a = 4\n",
    "b = 9\n",
    "\n",
    "for i in range(2, min(a, b) + 1):\n",
    "    print('Testing', i)\n",
    "    if a % i == 0 and b % i == 0:\n",
    "        print('Not coprime')\n",
    "        break\n",
    "else:\n",
    "    print('Coprime')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "def coprime(a, b):\n",
    "    for i in range(2, min(a, b) + 1):\n",
    "        if a % i == 0 and b % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "print(coprime(4, 9))\n",
    "print(coprime(3, 6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# Example 7\n",
    "def coprime2(a, b):\n",
    "    is_coprime = True\n",
    "    for i in range(2, min(a, b) + 1):\n",
    "        if a % i == 0 and b % i == 0:\n",
    "            is_coprime = False\n",
    "            break\n",
    "    return is_coprime\n",
    "print(coprime2(4, 9))\n",
    "print(coprime2(3, 6))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
